import {logout} from "./auth"

function createHandler(fun) {
    return function (...args) {
        args = JSON.parse(JSON.stringify(args))
        const {code, msg, data} = fun(...args)
        return new Promise((resolve, reject) => {
            if (code !== 0) {
                if (code === 101) {
                    logout()
                }
                reject(msg)
            } else if (data !== undefined) {
                resolve(JSON.parse(JSON.stringify(data)))
            } else {
                resolve()
            }
        })
    }
}

export default createHandler